function prepro_basic(subjectfile)

clc

eval(subjectfile);
    
outputdir   = '~/data/alebac_decperc/output/';
    
if exist(outputdir) == 0
	mkdir(outputdir);
end

dataset     = [subjectdata.subjectdir filesep subjectdata.datadir];


%% define trials 

cfg                     = [];
cfg.dataset             = dataset;
cfg.trialdef.eventtype	= 'UPPT001';
cfg.trialfun            = 'trlfun_decperc';

cfg_definetrial         = ft_definetrial(cfg);

save([outputdir filesep subjectdata.subjectnr '_cfg_definetrial'],'cfg_definetrial','-v7.3') 


%% automatic jump detection

cfg                                 = [];
cfg.trl                             = cfg_definetrial.trl;
cfg.continuous                      = 'yes';
cfg.datafile                        = dataset;
cfg.headerfile                      = dataset;

cfg.artfctdef.zvalue.channel        = 'MEG';
cfg.artfctdef.zvalue.cutoff         = 30;           

cfg.artfctdef.zvalue.trlpadding     = .1;
cfg.artfctdef.zvalue.artpadding     = .1;
cfg.artfctdef.zvalue.fltpadding     = .1;

cfg.artfctdef.zvalue.medianfilter   = 'yes';
cfg.artfctdef.zvalue.medianfiltord  = 9;
cfg.artfctdef.zvalue.absdiff        = 'yes';

cfg.artfctdef.zvalue.detrend        = 'yes';

cfg.artfctdef.zvalue.interactive    = 'yes';

[cfg, artifact_jump] = ft_artifact_zvalue(cfg);


% save cutoff zvalue in subject m-file
fid = fopen([subjectdata.mfiledir filesep subjectdata.subjectnr '.m'], 'At');
fprintf(fid,'\n%s\n',['%%% entered at ' datestr(now)]);
fprintf(fid,'%s',['subjectdata.artfctdef_jump_' session '.cutoff = ' ]);
fprintf(fid, '%d;', cfg.artfctdef.zvalue.cutoff);
fclose all;



%% automatic muscle detection

cfg                                 = [];
cfg.trl                             = cfg_definetrial.trl;
cfg.continuous                      = 'yes';
cfg.datafile                        = dataset;
cfg.headerfile                      = dataset;

cfg.artfctdef.zvalue.channel        = 'MRT*';
cfg.artfctdef.zvalue.cutoff         = 10;

cfg.artfctdef.zvalue.trlpadding     = .1;
cfg.artfctdef.zvalue.artpadding     = .1;
cfg.artfctdef.zvalue.fltpadding     = .1;

cfg.artfctdef.zvalue.bpfilter       = 'yes';
cfg.artfctdef.zvalue.bpfreq         = [110 140];
cfg.artfctdef.zvalue.bpfiltord      = 9;
cfg.artfctdef.zvalue.bpfilttype     = 'but';
cfg.artfctdef.zvalue.hilbert        = 'yes';
cfg.artfctdef.zvalue.boxcar         = 0.2;

cfg.artfctdef.zvalue.interactive    = 'yes';
cfg.artfctdef.zvalue.detrend        = 'yes';

[cfg, artifact_muscle] = ft_artifact_zvalue(cfg);

% save cutoff zvalue in subject m-file
fid = fopen([subjectdata.mfiledir filesep subjectdata.subjectnr '.m'], 'At');
fprintf(fid,'\n%s\n',['%%% entered at ' datestr(now)]);
fprintf(fid,'%s',['subjectdata.artfctdef_muscle_' session '.cutoff = ']);
fprintf(fid, '%d;', cfg.artfctdef.zvalue.cutoff);
fclose all; 


%% automatic EOG artifact detection

cfg                                 = [];
cfg.trl                             = cfg_definetrial.trl;
cfg.continuous                      = 'yes';
cfg.datafile                        = dataset;
cfg.headerfile                      = dataset;

cfg.artfctdef.zvalue.channel        = {'EEG057','EEG058'};
cfg.artfctdef.zvalue.cutoff         = 4;

cfg.artfctdef.zvalue.trlpadding     = .1;
cfg.artfctdef.zvalue.artpadding     = .1;
cfg.artfctdef.zvalue.fltpadding     = .1;

cfg.artfctdef.zvalue.bpfilter       = 'yes';
cfg.artfctdef.zvalue.bpfreq         = [1 15];
cfg.artfctdef.zvalue.bpfiltord      = 4;
cfg.artfctdef.zvalue.bpfilttype     = 'but';
cfg.artfctdef.zvalue.hilbert        = 'yes';

cfg.artfctdef.zvalue.interactive    = 'yes';
cfg.artfctdef.zvalue.detrend        = 'yes';

[cfg, artifact_eog] = ft_artifact_zvalue(cfg);

% save cutoff zvalue in subject m-file
fid = fopen([subjectdata.mfiledir filesep subjectdata.subjectnr '.m'], 'At');
fprintf(fid,'\n%s\n',['%%% entered at ' datestr(now)]);
fprintf(fid,'%s',['subjectdata.artfctdef_eog_' session '.cutoff = ']);
fprintf(fid, '%d;', cfg.artfctdef.zvalue.cutoff);
fclose all;


%% reject artifacts from trial definition

cfg                             = cfg_definetrial;
cfg.artfctdef.reject            = 'complete';
cfg.artfctdef.jump.artifact     = artifact_jump;
cfg.artfctdef.muscle.artifact   = artifact_muscle;
cfg.artfctdef.eog.artifact      = artifact_eog;

cfg_rejectartifact              = ft_rejectartifact(cfg);

save([outputdir filesep subjectdata.subjectnr '_cfg_rejectartifact'],'cfg_rejectartifact','-v7.3') 


%% preprocess data to disk

cfg                 = [];
cfg.dataset         = dataset;
cfg.trl             = cfg_rejectartifact.trl;
cfg.continuous      = 'yes';
cfg.channel         = 'MEG';
cfg.dftfilter       = 'no';
cfg.demean          = 'no';
cfg.detrend         = 'no';
cfg.lpfilter        = 'yes';
cfg.lpfreq          = 150;

data                = ft_preprocessing(cfg);


% resample
cfg                 = [];
cfg.resamplefs      = 300;
cfg.detrend         = 'no';

downSampledData     = ft_resampledata(cfg, data);

clear data


% denoise meg using the 3rd gradient
    
cfg                         = [];
cfg.gradient                = 'G3BR';
cfg.trials                  = 'all';
cfg.oldgradient             = downSampledData.grad;

denoisedData                = ft_denoise_synthetic(cfg, downSampledData);

clear dsdata


% save preprocessed data to disk

data = denoisedData;

save([outputdir filesep subjectdata.subjectnr '_data'],'data','-v7.3') 


end

